import React from 'react'

import {get} from '../../../fetch/get.js'
import {post} from '../../../fetch/post.js'
import OrderListComponent from '../../../components/OrderLIst'

import './style.scss'

class OrderList extends React.Component{
    constructor(props, context){
        super(props, context)
        this.state={
            data: []
        }
    }
    render() {
        return (
            <div className="order-list">
                <h2>您的订单</h2>
                {
                    this.state.data.length
                    ? <OrderListComponent data={this.state.data} submitComent={this.submitComent.bind(this)}></OrderListComponent>
                    : ''
                }
            </div>
        )
    }
    componentDidMount() {
        const userName = this.props.userName
        if(userName) {
            this.loadOrderList(userName)
        }
        
    }
    loadOrderList(userName) {
        const result = get('/api/orderlist/' + userName)
        result.then(res => {
            return res.json()
        }).then(json => {
            this.setState({
                data: json.data
            })
        })
    }
    //提交评价
    submitComent(id, value, callback) {
        const result = post('/api/submitComent', {id: id, value: value})
        result.then(res => {
            return res.json()
        }).then(json => {
            callback()
        })
    }
}

export default OrderList